package service

import (
	"context"

	v1 "realworld/api/realworld/v1"
	"realworld/internal/biz"
)

// GreeterService is a greeter service.
type RealWorldService struct {
	v1.UnimplementedRealworldServer

	uc *biz.GreeterUsecase
}

// NewGreeterService new a greeter service.
func NewRealWorldService(uc *biz.GreeterUsecase) *RealWorldService {
	return &RealWorldService{uc: uc}
}

// SayHello implements realworld.GreeterServer.
func (s *RealWorldService) SayHello(ctx context.Context, in *v1.HelloRequest) (*v1.HelloReply, error) {
	g, err := s.uc.CreateGreeter(ctx, &biz.Greeter{Hello: in.Name})
	if err != nil {
		return nil, err
	}
	return &v1.HelloReply{Message: "Hello " + g.Hello}, nil
}

// 新增 Registration 接口
func (s *RealWorldService) Registration(ctx context.Context, in *v1.RegistrationRequest) (*v1.RegistrationReply, error) {
	createdUser, err := s.uc.RegistrationInterface(ctx, &biz.Registration{
		Email:    in.User.Email,
		Password: in.User.Password,
	})
	if err != nil {
		return nil, err
	}

	return &v1.RegistrationReply{
		User: &v1.RegistrationReply_User{
			Email:    createdUser.Email,
			Token:    createdUser.Token,
			Username: createdUser.Username,
			Bio:      createdUser.Bio,
			Image:    createdUser.Image,
		},
	}, nil
}
